# GT, 30/04/2021 ;
library(mice)
library(tidyverse)

#-------------------------------------------------------------------------------
# PREPARE WIDE ESS DATA ;
rm(list=ls())

# LOAD DATA, KEEP ONLY COMLETE VIGNETTES, AND TREATMENTS
ess <- read_csv("REC_ESS8_CONTEXT.csv") %>%
  select(cntry, sanction_pay, sanction_edu, sanction_unp, vignette, age, age2, 
         female, edu_bmask, income, cont_unemp, prev_unemp, subj_unemp,
         pplfair, pplhlp, ppltrst, lrscale, sbbsntx, sbeqsoc, sblazy, sblwcoa, 
         sbprvpv, sbstrec, z_c_ethfrac, z_c_lngfrac, z_c_relfrac, z_c_gini_2016, 
         z_c_medage_2016, z_c_gdppc_2016, z_c_soexgni_2015, z_c_unraall_2016, 
         z_oecd_protect, z_oecd_repl)  %>%
  filter(!is.na(vignette)) %>%
  filter(!is.na(sanction_pay)) %>%
  filter(!is.na(sanction_edu)) %>%
  filter(!is.na(sanction_unp))
dim(ess)

# TAKE CARE OF FACTOR VARIABLES ;
# PREPARE CATEGORICAL OUTCOME VARIABLE ;
ess$sanction_pay <- factor(ess$sanction_pay, ordered=TRUE)
ess$sanction_edu <- factor(ess$sanction_edu, ordered=TRUE)
ess$sanction_unp <- factor(ess$sanction_unp, ordered=TRUE)

# PREPARE ALTERNATIVE METRIC OUTCOME VARIABLE ;
ess$sanction_pay_no <- recode(ess$sanction_pay, "1"=0, "2"=25,"3"=50,"4"=100)
ess$sanction_edu_no <- recode(ess$sanction_edu, "1"=0, "2"=25,"3"=50,"4"=100)
ess$sanction_unp_no <- recode(ess$sanction_unp, "1"=0, "2"=25,"3"=50,"4"=100)

# TREATMENT ;
ess$vignette <- relevel(factor(ess$vignette), ref="2")

# INDEP. VARIABLES ;
ess$edu_bmask <- factor(ess$edu_bmask, ordered=TRUE)
ess$eduincome <- factor(ess$income, ordered=TRUE)

#-------------------------------------------------------------------------------
# MICE: IMPUTE WIDE ESS DATA ;
# VARIABLES NOT TO BE IMPUTED ;
#no_imp  <- c("z_c_ethfrac", "z_c_lngfrac", "z_c_relfrac", "z_c_gini_2016", 
#           "z_c_medage_2016", "z_c_gdppc_2016", "z_c_soexgni_2015",
#           "z_c_unraall_2016", "z_oecd_protect", "z_oecd_repl")

#ess_m0  <- mice(ess, maxit=0)
#ess_m   <- ess_m0$method
#ess_m[names(ess_m) %in% no_imp]  <- ""

#ess_p   <- ess_m0$predictorMatrix
#ess_p[, colnames(ess_p) %in% no_imp] <- 0   

#ess_imp_5  <- mice(ess, predictorMatrix=ess_p, method=ess_m, m=5)
#ess_imp_20 <- mice(ess, predictorMatrix=ess_p, method=ess_m, m=20)
#ess_imp_50 <- mice(ess, predictorMatrix=ess_p, method=ess_m, m=50)

#-------------------------------------------------------------------------------
# PREPARE LONG ESS DATA ;
ess_long <- read_csv("REC_ESS8_LONG_CONTEXT.csv") %>%
  select(cntry, sanction, situation, vignette,
         age, age2, female, edu_bmask, income, cont_unemp, prev_unemp, subj_unemp,
         pplfair, pplhlp, ppltrst, lrscale, sbbsntx, sbeqsoc, sblazy, sblwcoa, 
         sbprvpv, sbstrec, z_c_ethfrac, z_c_lngfrac, z_c_relfrac, z_c_gini_2016, 
         z_c_medage_2016, z_c_gdppc_2016, z_c_soexgni_2015, z_c_unraall_2016, 
         z_oecd_protect, z_oecd_repl)  %>%
  filter(!is.na(vignette)) %>%
  filter(!is.na(sanction)) 
dim(ess_long)    

# TAKE CARE OF FACTOR VARIABLES ;
# CONVERT VARIABLES TO ORDERED FACTORS ;
ess_long$sanction    <- factor(ess_long$sanction, ordered=TRUE)
ess_long$sanction_no <- recode(ess_long$sanction, "1"=0, "2"=25,"3"=50,"4"=100)
ess_long$situation   <- factor(ess_long$situation, ordered=TRUE)

# TREATMENT ;
ess_long$vignette    <- relevel(factor(ess_long$vignette), ref="2")

# INDEP. VARIABLES ;
ess$edu_bmask <- factor(ess$edu_bmask)

#-------------------------------------------------------------------------------
# MICE: IMPUTE LONG ESS DATA ;
#ess_long_m0   <- mice(ess_long, maxit=0)
#ess_long_m    <- ess_long_m0$method
#ess_long_m[names(ess_long_m) %in% no_imp] <- ""

#ess_long_p    <- ess_long_m0$predictorMatrix
#ess_long_p[, colnames(ess_long_p) %in% no_imp] <- 0

#ess_long_imp_20 <- mice(ess_long, predictorMatrix=ess_long_p, method=ess_long_m, m=20)
#ess_long_check <- multiple(ess_long_imp,1)

#-------------------------------------------------------------------------------
# SAVE EMPIRICAL AND IMPUTED DATA ;
save.image("DATA_ESS.RData")
